#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 5e4 + 5;
const int M = 1e9 + 7;

string x;
int n;
string str[N];

map<char, char> m;

bool cmp(const string& a, const string& b) {
    string c, d;
    for (auto ch : a) {
        c += m[ch];
    }

    for (auto ch : b) {
        d += m[ch];
    }
    return c < d;
}
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false), cin.tie(0);

    cin >> x >> n;
    for (int i = 0; i < n; i++)
        cin >> str[i];

    for (int i = 0; i < 26; i++) {
        m[x[i]] = 'a' + i;
    }

    sort(str, str + n, cmp);

    for (int i = 0; i < n; i++) cout << str[i] << endl;
    return 0;
}